/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2018-2019. All rights reserved.
 *
 * @file    mapi_vgs_client.c
 * @brief   vgs client function
 * @author  HiMobileCam NDK develop team
 * @date  2019-8-7
 */

#include "mpi_vgs.h"
#include "msg_vgs.h"
#include "msg_define.h"
#include "msg_wrapper.h"
#include "hi_mapi_sys.h"
#include "mapi_comm_inner.h"
#include "hi_mapi_vgs_define.h"


HI_S32 HI_MAPI_VGS_BeginJob(HI_HANDLE *vgsHdl)
{
    CHECK_MAPI_NULL_PTR_RET(HI_MAPI_MOD_VGS, vgsHdl, HI_MAPI_VGS_ENULL_PTR);

    HI_S32 ret;
    HI_U32 u32ModFd = MODFD(HI_MAPI_MOD_VGS, 0, 0);
    ret = MAPI_SendSync(u32ModFd, MSG_CMD_VGS_BEGIN_JOB, vgsHdl, sizeof(HI_HANDLE),
        HI_NULL);
    if (ret != HI_SUCCESS) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_VGS, "begin job fail, ret:%x\n", ret);
        return ret;
    }

    return HI_SUCCESS;
}

HI_S32 HI_MAPI_VGS_AddScaleTask(HI_HANDLE vgsHdl, const HI_MAPI_VGS_ScaleTaskAttr *scaleTaskAttr)
{
    CHECK_MAPI_NULL_PTR_RET(HI_MAPI_MOD_VGS, scaleTaskAttr, HI_MAPI_VGS_ENULL_PTR);
    CHECK_MAPI_BOOL_RET(HI_MAPI_MOD_VGS, vgsHdl != (HI_U32)HI_INVALID_HANDLE, HI_MAPI_VGS_EHANDLE_ILLEGAL);

    MAPI_PRIV_DATA_S msgPriData;
    HI_MAPI_VGS_ScaleTaskAttr vgsScaleTaskAttr;
    HI_S32 ret;

    ret = memcpy_s(&vgsScaleTaskAttr, sizeof(HI_MAPI_VGS_ScaleTaskAttr), scaleTaskAttr,
        sizeof(HI_MAPI_VGS_ScaleTaskAttr));
    MAPI_CHECK_RET(HI_MAPI_MOD_VGS, "memcpy_s", ret);

    msgPriData.as32PrivData[0] = vgsHdl;
    HI_U32 modFd = MODFD(HI_MAPI_MOD_VGS, 0, 0);
    ret = MAPI_SendSync(modFd, MSG_CMD_VGS_ADD_SCALETASK, &vgsScaleTaskAttr,
        sizeof(HI_MAPI_VGS_ScaleTaskAttr), &msgPriData);
    if (ret != HI_SUCCESS) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_VGS, "begin job fail, ret:%x\n", ret);
        return ret;
    }

    return HI_SUCCESS;
}


HI_S32 HI_MAPI_VGS_EndJob(HI_HANDLE vgsHdl)
{
    CHECK_MAPI_BOOL_RET(HI_MAPI_MOD_VGS, vgsHdl != (HI_U32)HI_INVALID_HANDLE, HI_MAPI_VGS_EHANDLE_ILLEGAL);

    HI_S32 ret;
    HI_U32 u32ModFd = MODFD(HI_MAPI_MOD_VGS, 0, 0);
    ret = MAPI_SendSync(u32ModFd, MSG_CMD_VGS_END_JOB, &vgsHdl, sizeof(HI_HANDLE), HI_NULL);
    if (ret != HI_SUCCESS) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_VGS, "end job fail, ret:%x\n", ret);
        return ret;
    }

    return HI_SUCCESS;
}

HI_S32 HI_MAPI_VGS_CanceJob(HI_HANDLE vgsHdl)
{
    CHECK_MAPI_BOOL_RET(HI_MAPI_MOD_VGS, vgsHdl != (HI_U32)HI_INVALID_HANDLE, HI_MAPI_VGS_EHANDLE_ILLEGAL);

    HI_S32 ret;
    HI_U32 u32ModFd = MODFD(HI_MAPI_MOD_VGS, 0, 0);
    ret = MAPI_SendSync(u32ModFd, MSG_CMD_VGS_CANCE_JOB, &vgsHdl, sizeof(HI_HANDLE), HI_NULL);
    if (ret != HI_SUCCESS) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_VGS, "end job fail, ret:%x\n", ret);
        return ret;
    }

    return HI_SUCCESS;
}
